package br.trainee.web.bo.componentes;

import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;

import br.trainee.web.bo.impl.EntityBOImpl;
import br.trainee.web.dao.impl.componentes.AutoCompleteDAOImpl;
import br.trainee.web.dao.service.IEntityDAO;

public class AutoCompleteBO extends EntityBOImpl implements Serializable{

	private static final long serialVersionUID = 1L;

	@Inject
	private AutoCompleteDAOImpl dao;

	@Inject
	private Logger log;
	
	private Class classeObjeto;
	
	@PersistenceContext(unitName="WebJsf")
	private EntityManager em;
	
	@Resource
	private UserTransaction userTransaction;

	@Override
	public IEntityDAO getEntityDao() {
		
		// Criando DAO com valor null.
		AutoCompleteDAOImpl dao = null;
		
		try {

			dao = (AutoCompleteDAOImpl) classeObjeto.newInstance();
			
			dao.setClasseObjeto(classeObjeto);
			
		} catch (InstantiationException e) {

			log.log(Level.SEVERE, e.getMessage());
			
		} catch (IllegalAccessException e) {

			log.log(Level.SEVERE, e.getMessage());
			
		}
		
		return dao;
	}

	@Override
	public Logger getLogger() {
		return log;
	}

	public EntityManager getEm() {
		return em;
	}
	
	// Metodo que chama uma busca generica do DAO e retorna uma List 
	public List retornarLista(String className, String columnName, String parameterReturn, String columnStatusName, Integer columnStatusValue, String atributeStatusName){
		
		// Criando a List com o valor null.
		List lista = null;
		
		try {
			
			// Chamando o consultar dinamico.
			lista = dao.consultarDinamico(className, columnName, parameterReturn, columnStatusName, columnStatusValue, atributeStatusName);
			
		} catch (Exception e) {
			
			log.log(Level.SEVERE, e.getMessage());
			
			// Caso não encontre nenhum valor a lista recebe null.
			lista = null;
			
		}
		
		// Retorna a List.
		return lista;
	}

	public void setEm(EntityManager em) {
		this.em = em;
	}

	public UserTransaction getUserTransaction() {
		return userTransaction;
	}

	public void setUserTransaction(UserTransaction userTransaction) {
		this.userTransaction = userTransaction;
	}
	
	public void setClasseObjeto(Class classeObjeto){
		this.classeObjeto = classeObjeto;
	}
	
}
